package com.lb.jwitter.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.StyledEditorKit.BoldAction;

import org.springframework.context.ApplicationContext;

import sun.security.krb5.internal.tools.Ktab;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocTienQuyet;
import com.lb.jwitter.service.TblCTDTMonHocTienQuyetService;
import com.lb.jwitter.service.TblCTDTThayTheMonHoc;
import com.lb.jwitter.service.TblCTDTThayTheMonHocService;
import com.lb.jwitter.service.TblDKMHHocVienMonHocDuocDKService;
import com.lb.jwitter.service.TblRangBuocChuyenNganhCam;
import com.lb.jwitter.service.TblRangBuocChuyenNganhCamService;
import com.lb.jwitter.service.TblRangBuocHeDaoTaoNhomHocService;
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienDTB;
import com.lb.jwitter.service.TblThongTinHocVienDTBService;
import com.lb.jwitter.service.TblThongTinHocVienMonHoc;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;
import com.lb.jwitter.service.TblThongTinTKBChiTiet;
import com.lb.jwitter.service.TblThongTinTKBChiTietService;

public class KiemTraDangKy {
	//kiem tra 2 mon hoc khac chi nhanh cach nhau it nhat 3 tiet
	public static boolean kiemTraThoiGianGianCachGiua2CoSo(KetQuaDangKy dangKyMoi,
			List<KetQuaDangKy> ketQuaDaDangKys, Message msg) {

		for (KetQuaDangKy ketQuaDangKy : ketQuaDaDangKys) {
			if (dangKyMoi.getKetqua().getId() == null
					|| !ketQuaDangKy.getKetqua().getId().equals(
							dangKyMoi.getKetqua().getId())) {
				if (ketQuaDangKy.getKetqua().getMaDieuChinh().equals("BS")
						|| ketQuaDangKy.getKetqua().getMaDieuChinh().equals(
								"HC")) {

					//duyet tung chi tiet da dang ky
					for (TblThongTinTKBChiTiet chiTietDaDK : ketQuaDangKy
							.getThoiKhoaBieuDuKien().getChiTiets()) {
						
						//duyet chi tiet moi dang ky
						for (TblThongTinTKBChiTiet chiTietDK : dangKyMoi
								.getThoiKhoaBieuDuKien().getChiTiets()) {

							//neu khac co so hoc
							if(chiTietDK.getMaCoSo() != null && chiTietDaDK.getMaCoSo() != null 
									&& !chiTietDK.getMaCoSo().equals("")
									&& !chiTietDK.getMaCoSo().equals(chiTietDaDK.getMaCoSo())){
								
								//neu cung ngay
								if (chiTietDaDK.getNgayHocId().getId().equals(
										chiTietDK.getNgayHocId().getId())) {
	
									int tietKetThucDaDK = chiTietDaDK.getTietBatDauId().getSTT()
											+ chiTietDaDK.getSoTietHoc() - 1;
									int tietKetThucDK = chiTietDK.getTietBatDauId()
											.getSTT()
											+ chiTietDK.getSoTietHoc() - 1;
	
									//--neu thoi gian gian cach khong du: 3tiet
									//neu (tietBD DK moi sau tietBD da DK và tietBD DK moi sau tietKT da DK duoi 3 tiet)
									//hoac (tietBD DK moi truoc tietBD da DK va tietKT DK moi truoc tietBD da DK 3 tiet)
									if ((chiTietDK.getTietBatDauId().getSTT() >= chiTietDaDK.getTietBatDauId().getSTT()  
											&& chiTietDK.getTietBatDauId().getSTT() <= tietKetThucDaDK + 3)
											|| (chiTietDK.getTietBatDauId().getSTT() <= chiTietDaDK.getTietBatDauId().getSTT()  
													&& tietKetThucDK + 3 >= chiTietDaDK.getTietBatDauId().getSTT() )) {
	
										msg.setCode("ERROR");
										msg.setMsg("Không đủ thời gian giãn cách giữa 2 cơ sở - Môn học " 
												+ ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getMa() + " - " 
												+ ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getTen());
										return true;
									}// end if
								}// end if
							}//end if maCoSO
						}// end for
					}
				}
			}
		}
		return false;
	}
	
	
	//--------------------------------------------------------------------
	//kiem tra ngoai le
	public static boolean kiemTraNgoaiLe(KetQuaDangKy dangKyMoi, List<KetQuaDangKy> ketQuaDaDangKys, Message msg){
		
		//-----------------------------------------------------------------------
		//kiem tra he VIetPhap
		boolean svVietPhap = false;
		boolean monHocVietPhap = false;
		boolean nhomVietPhap = false;
		
		try{
			if(dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId().getMa().equals("VP"))
				svVietPhap = true;
			
			if(dangKyMoi.getThoiKhoaBieuDuKien().getNhomLop().getMonHocId().getMa().startsWith("P"))
				monHocVietPhap = true;
			
			if(dangKyMoi.getThoiKhoaBieuDuKien().getNhomLop().getMa().startsWith("P"))
				nhomVietPhap = true;
			
		}catch (Exception e) {
			
		}
		
		//neu la SV viet phap
		if(svVietPhap){
				if(!nhomVietPhap){
					//sinh vien VP phai dang ky nhom VP
					msg.setMsg("Sinh vi\u00EAn Vi\u00EA\u0323t - Pha\u0301p pha\u0309i \u0111\u0103ng ky\u0301 va\u0300o nho\u0301m Vi\u00EA\u0323t - Pha\u0301p!!!");
					return true;
				}
 		}
		//sinh vien lop thuong
		else{
			//dang ky nhom VP nhung la mon hoc khong thuoc chuong trinh VP
			if(nhomVietPhap && !monHocVietPhap){
				//Sinh vien ngoai khong duoc dang ky vao nhom Viet-Phap
				msg.setMsg("Ba\u0323n kh\u00F4ng \u0111\u01B0\u01A1\u0323c phe\u0301p \u0111\u0103ng ky\u0301 va\u0300o nho\u0301m Vi\u00EA\u0323t - Pha\u0301p!!!");
				return true;
			}
		}
		
		return false;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	//---------------------------------------------------------------------
	//tinh tong so tin chi no cua hoc vien
	private static double tongSoTinChiNo(TblThongTinHocVien hocVien, CTDT ctdt,  ApplicationContext context){

		TblThongTinHocVienDTBService tblThongTinHocVienDTBService = (TblThongTinHocVienDTBService) context.getBean("tbl_ThongTin_HocVien_DTBClient");
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		
		//tong so TC BB da hoan thanh
		{
			ConditionReport con = new ConditionReport();
			con.setCol("hocVienId.id");
			con.setValue1(hocVien.getId());
			con.setOperator("eq");
			cons.add(con);
		}
		{
			ConditionReport con = new ConditionReport();
			con.setCol("maHocKy");
			con.setValue1("desc");
			con.setOperator("order");
			cons.add(con);
		}
		List<TblThongTinHocVienDTB> diemTB = tblThongTinHocVienDTBService.findtblThongTinHocVienDTBByCondition(cons, 1, 1);
		
		double soTCNo = 0;
		double soTCNganh = 0;
		double soTCTL = 0;
		try{
			soTCNganh = ctdt.getMonHocs().get(0).getKhoiNganhId().getSoTinChi();
			soTCTL = diemTB.get(0).getTCTLNganh();
		}catch(Exception e){}
		
		
		soTCNo = soTCNganh - soTCTL;
		
		System.out.println("so tc no = " + soTCNo);
		return soTCNo;
	}
	
	//------------------------------------------------------------------------------------------
	//kiem tra mon hoc TTTN
	public static boolean kiemTraKhongDuDKMonTTTN(KetQuaDangKy dangKyMoi, List<KetQuaDangKy> ketQuaDaDangKys, 
			 CTDT ctdt,  ApplicationContext context, Message msg) {
		
		double soTCNoToiDa = 50;

		if(dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId() != null 
				&& dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId().getMa() != null)
		{
			if(dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId().getMa().equals("CD"))
				soTCNoToiDa = 25;
		}
		
		//-----------------------------------------------
		//tong so tin chi no
		double soTCNo = tongSoTinChiNo(dangKyMoi.getKetqua().getHocVienId(), ctdt, context);
		
		//so tin chi no > 50
		if(soTCNo > soTCNoToiDa){

			//so tin chi no phai duoi 50 tin chi
			msg.setMsg("Bạn đang nợ " +soTCNo+ " tín chỉ. Số tín chỉ nợ phải dưới "+ soTCNoToiDa+" tín chỉ!!!");
			return true;
		}
		
		//-------------------------------------------
		//
		
		return false;
	}
	//------------------------------------------------------------------------------------------
	//kiem tra mon hoc LVTN
	public static boolean kiemTraKhongDuDKMonLVTN(KetQuaDangKy dangKyMoi, List<KetQuaDangKy> ketQuaDaDangKys, 
			 CTDT ctdt,  ApplicationContext context, Message msg) {
		
		double soTCNoToiDa = 21;

		if(dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId() != null 
				&& dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId().getMa() != null)
		{
			if(dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId().getMa().equals("CD"))
				soTCNoToiDa = 25;
		}
		
		//---------------------------
		//So tin chi chua dat duoi 21TC

		//-----------------------------------------------
		//tong so tin chi no
		double soTCNo = tongSoTinChiNo(dangKyMoi.getKetqua().getHocVienId(), ctdt, context);
		
		if(soTCNo > soTCNoToiDa){
			//so tin chi no phai duoi 21 tin chi

			msg.setMsg("Số tín chỉ nợ phải dưới "+ soTCNoToiDa+" tín chỉ!!!");
			return true;
		}
			
		/************************************
		 * Da kiem tra trong phan mon hoc tien quyet-song hanh
		 ************************************/
//		//hoan tat cac mon DA, TTTN
//		for(TblCTDTChuyenNganhMonHoc c : ctdt){
//			if(c.getMonHocId().getLoai() != null && (c.getMonHocId().getLoai().equals("DA") || c.getMonHocId().getLoai().equals("TTTN") )){
//				cons.clear(); columns.clear();
//				{
//					ConditionReport con = new ConditionReport();
//					con.setCol("hocVienId.id");
//					con.setValue1(dangKyMoi.getKetqua().getHocVienId().getId());
//					con.setOperator("eq");
//					cons.add(con);
//				}
//				{
//					ConditionReport con = new ConditionReport();
//					con.setCol("monHocId.id");
//					con.setValue1(c.getMonHocId().getId());
//					con.setOperator("eq");
//					cons.add(con);
//				}
//				{
//					ConditionReport con = new ConditionReport();
//					
//					con.setType("String");
//					con.setCol("trangThai");
//					con.setValue1("PASS;UNKN"); 
//					con.setOperator("in");
//					cons.add(con);
//				}
//				int rows =  tblThongTinHocVienMonHocService
//						.counttblThongTinHocVienMonHocByCondition(cons);
//				
//				if(rows<= 0){
//					msg.setMsg("Chua hoan thanh cac mon do an mon hoc hoac thuc tap tot nghiep!!!");
//					return true;
//				}
//			}
//		}
		
//		//hoan tat  mon phu: NgoaiNgu
//		cons.clear(); columns.clear();
//		{
//			ConditionReport con = new ConditionReport();
//			con.setCol("hocVienId.id");
//			con.setValue1(dangKyMoi.getKetqua().getHocVienId().getId());
//			con.setOperator("eq");
//			cons.add(con);
//		}
//		{
//			ConditionReport con = new ConditionReport();
//			con.setCol("monHocId.maMonHoc");
//			//MA MON HOC NGOAI NGU 450
//			con.setValue1("NN450");
//			con.setOperator("eq");
//			cons.add(con);
//		}
//		{
//			ConditionReport con = new ConditionReport();
//			
//			con.setType("String");
//			con.setCol("trangThai");
//			con.setValue1("PASS;UNKN"); 
//			con.setOperator("in");
//			cons.add(con);
//		}
//		int kqNN =  tblThongTinHocVienMonHocService
//				.counttblThongTinHocVienMonHocByCondition(cons);
//		
//		if(kqNN  <= 0){
//			msg.setMsg("Chua hoan thanh chung chi ngoai ngu!!!");
//			return true;
//		}
//
//		//hoan tat  mon phu: CTXH
//		cons.clear(); columns.clear();
//		{
//			ConditionReport con = new ConditionReport();
//			con.setCol("hocVienId.id");
//			con.setValue1(dangKyMoi.getKetqua().getHocVienId().getId());
//			con.setOperator("eq");
//			cons.add(con);
//		}
//		{
//			ConditionReport con = new ConditionReport();
//			con.setCol("monHocId.maMonHoc");
//			//MA MON HOC CTXH
//			con.setValue1("CTXH");
//			con.setOperator("eq");
//			cons.add(con);
//		}
//		{
//			ConditionReport con = new ConditionReport();
//			
//			con.setType("String");
//			con.setCol("trangThai");
//			con.setValue1("PASS;UNKN"); 
//			con.setOperator("in");
//			cons.add(con);
//		}
//		int kqCTXH =  tblThongTinHocVienMonHocService
//				.counttblThongTinHocVienMonHocByCondition(cons);
//		
//		if(kqCTXH  <= 0){
//			msg.setMsg("Chua hoan thanh du ngay cong tac xa hoi!!!");
//			return true;
//		}
		
		return false;
	}
	
	//--------------------------------------------------------------------------
	//kiem tra he dao tao - nhom lop
	public static boolean kiemTraNhomLopKhongDuocDangKy(KetQuaDangKy dangKyMoi,  ApplicationContext context, Message msg) {
		TblRangBuocHeDaoTaoNhomHocService tblRangBuocHeDaoTaoNhomHocService = 
				(TblRangBuocHeDaoTaoNhomHocService) context.getBean("tbl_RangBuoc_HeDaoTao_NhomHocClient");
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		List<String> columns = new ArrayList<String>();
		
		if(dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId()==null)
			return false;
		
		//----------------------------------------------------------------------
		{
			ConditionReport con = new ConditionReport();
			con.setCol("heDaoTaoId.id");
			con.setValue1(dangKyMoi.getKetqua().getHocVienId().getHeDaoTaoId().getId());
			con.setOperator("eq");
			cons.add(con);
		}
		{
			
			Pattern p = Pattern.compile("\\d+");
		    Matcher m = p.matcher(dangKyMoi.getKetqua().getNLMHId().getMa());

			String nhomHoc = dangKyMoi.getKetqua().getNLMHId().getMa();
		    while(m.find()){
		    	nhomHoc = nhomHoc.replaceAll(m.group(), "[%]");
		    }

			ConditionReport con = new ConditionReport();
			con.setCol("nhomHoc");
			con.setValue1(nhomHoc); 
			con.setOperator("like");
			cons.add(con);
		}
		
		int rows =  tblRangBuocHeDaoTaoNhomHocService
				.counttblRangBuocHeDaoTaoNhomHocByCondition(cons);
		
		if(rows <= 0){
			//Ban khong duoc phep dang ky vao nhom lop nay
			msg.setMsg("Bạn không được phép đăng ký vào nhóm lớp này!!!");
			return true;
		}
		
		return false;
	}
	
	
	// //////////////////////////////////////////////////////////////////////////////////////
	public static boolean kiemTraTrungTKB(KetQuaDangKy dangKyMoi,
			List<KetQuaDangKy> ketQuaDaDangKys, Message msg) {

		//duyet tung ket qua dang ky
		for (KetQuaDangKy ketQuaDangKy : ketQuaDaDangKys) {
			//neu ket qua dang ky khong trung voi ketqua dang ky moi
			if (dangKyMoi.getKetqua().getId() == null
					|| !ketQuaDangKy.getKetqua().getId().equals(
							dangKyMoi.getKetqua().getId())) {
				//neu ket qua dang ky hien tai la dang ky moi hoac bo sung, chua bi huy
				if (ketQuaDangKy.getKetqua().getMaDieuChinh().equals("BS")
						|| ketQuaDangKy.getKetqua().getMaDieuChinh().equals(
								"HC")) {

					//duyet tung chi tiet TKB cua ket uqa dang ky hien tai
					for (TblThongTinTKBChiTiet chiTietDaDK : ketQuaDangKy
							.getThoiKhoaBieuDuKien().getChiTiets()) {
						//duyet tung chi tiet TKB cua dang ky moi
						for (TblThongTinTKBChiTiet chiTietDK : dangKyMoi
								.getThoiKhoaBieuDuKien().getChiTiets()) {

							//neu so tiet hoc bang 0
							if(chiTietDK.getSoTietHoc() == 0 || chiTietDK.getTietBatDauId().getMa().equals("0")){
								break;
							}
							if(chiTietDaDK.getSoTietHoc() == 0 || chiTietDaDK.getTietBatDauId().getMa().equals("0")){
								break;
							}
							
							//neu cung ngay
							if (chiTietDaDK.getNgayHocId().getId().equals(
									chiTietDK.getNgayHocId().getId())) {

								//tiet ket thuc cua ket qua hien tai
								int tietKetThucDaDK = chiTietDaDK
										.getTietBatDauId().getSTT()
										+ chiTietDaDK.getSoTietHoc() - 1;
								//tiet ket thuc cua ket qua dk moi
								int tietKetThucDK = chiTietDK.getTietBatDauId()
										.getSTT()
										+ chiTietDK.getSoTietHoc() - 1;

								//neu tiet ket thuc va tiet bat dau cua 2 ket qua co su chồng lấ
								//se kiem tra tuan hoc
								if ((chiTietDK.getTietBatDauId().getSTT() >= chiTietDaDK
										.getTietBatDauId().getSTT() && chiTietDK
										.getTietBatDauId().getSTT() <= tietKetThucDaDK)
										|| (tietKetThucDK >= chiTietDaDK
												.getTietBatDauId().getSTT() && tietKetThucDK <= tietKetThucDaDK)
										|| (chiTietDK.getTietBatDauId()
												.getSTT() <= chiTietDaDK
												.getTietBatDauId().getSTT() && tietKetThucDK >= tietKetThucDaDK)) {
									//neu bi trung 1 tuan hoc --> trung TKB
									if(chiTietDaDK.getTuanHoc() != null && chiTietDK.getTuanHoc() != null){
										
										if((chiTietDaDK.getTuanHoc() & chiTietDK.getTuanHoc()) != 0){
											msg.setCode("ERROR");
											msg.setMsg("Trùng thời khóa biểu môn học :" 
												+ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getMa() 
												+ "-" 
												+ ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getTen());
											
											return true;
										}
									}else{
										return true;
									}
									
								}// end if
							}// end if
						}// end for
					}
				}
			}
		}
		return false;
	}
	
	
	//------------------------------------------------------------------------------------------
	//kiem tra vuot so tin chi cho phep
	public static boolean kiemTraVuotSoTCDangKyToiDa(KetQuaDangKy dangKyMoi,
			List<KetQuaDangKy> ketQuaDaDangKys,double soTCDangKyToiDa, double soTCDieuChinh, ApplicationContext context, Message msg) {

		TblThongTinHocVienDTBService tblThongTinHocVienDTBService = (TblThongTinHocVienDTBService) context.getBean("tbl_ThongTin_HocVien_DTBClient");
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		
		//tong so TC BB da hoan thanh
		{
			ConditionReport con = new ConditionReport();
			con.setCol("hocVienId.id");
			con.setValue1(dangKyMoi.getKetqua().getHocVienId().getId());
			con.setOperator("eq");
			cons.add(con);
		}
		{
			ConditionReport con = new ConditionReport();
			con.setCol("maHocKy");
			con.setValue1("desc");
			con.setOperator("order");
			cons.add(con);
		}
		//get dtb hocvien
		List<TblThongTinHocVienDTB> diemTB = tblThongTinHocVienDTBService.findtblThongTinHocVienDTBByCondition(cons, 1, 1);
		
//		soTCDangKyToiDa = 21;
		
		/*
		 * Lay ra so TC DK toi da cua sinh vien
		 * Neu so TC Toida trong dot DK >0 --> lay soTCTD trong dotDK
		 * Neu soTCTD trong dotDk ==0 - > kiem tra so TCTD cua Khoahoc va HeDaotao trong bang ctdt_config
		 * Neu khong co trong bang CTDT_Config thi lay mac dinh la 21 tc
		 */
		//kiem tra soTCTD trong dotDK cua HV
		
		double soTCTTTN = 0;
		double soTCLVTN = 0;
		
		double soTCDangKy = dangKyMoi.getKetqua().getNLMHId().getMonHocId().getSoTinChi();
		double tongSoTCDangKy = 0;
		
		boolean lvtnYN = false;
		boolean tttnYN = false;
		
		boolean svKhaGioi = false;
		boolean svYeu = false;
		
		try
		{ 
			if( diemTB.get(0).getDTBTichLuy() > 7.0)
				svKhaGioi = true;
			else if( diemTB.get(0).getDTBTichLuy() < 5.0)
				svYeu = true;
		}catch(Exception e){}
		
		//mon moi dang ky la LVTN
		if(dangKyMoi.getKetqua().getNLMHId().getMonHocId().getLoai() != null 
				&& (dangKyMoi.getKetqua().getNLMHId().getMonHocId().getLoai().equals("LV") || dangKyMoi.getKetqua().getNLMHId().getMonHocId().getLoai().equals("TL") ) )
		{
			lvtnYN = true;
			soTCLVTN = dangKyMoi.getKetqua().getNLMHId().getMonHocId().getSoTinChi();
		}
		
		
//		boolean hkChan = false;
//		if(dangKyMoi.getKetqua().getDotDKId().getHocKyId().getMa().startsWith("2"))
//			hkChan = true;
//		
//		//mon moi DK la TTTN
//		if(dangKyMoi.getKetqua().getNLMHId().getMonHocId().getLoai() != null 
//				&& dangKyMoi.getKetqua().getNLMHId().getMonHocId().getLoai().equals("TTTN") )
//		{
//			tttnYN = true;
//			soTCTTTN = dangKyMoi.getKetqua().getNLMHId().getMonHocId().getSoTinChi();
//		}
		

		for (KetQuaDangKy ketQuaDangKy : ketQuaDaDangKys) {
			if(ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getLoai() == null || !ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getLoai().equals("TTNT"))
				tongSoTCDangKy += ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getSoTinChi();
			
			if(ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getLoai() != null 
					&& (ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getLoai().equals("LV")
							||ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getLoai().equals("TLTN")) )
			{
				lvtnYN = true;
				soTCLVTN = ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getSoTinChi();
			}
			
//			else if(ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getLoai() != null 
//					&& ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getLoai().equals("TTTN") )
//			{
//				tttnYN = true;
//				soTCTTTN = ketQuaDangKy.getKetqua().getNLMHId().getMonHocId().getSoTinChi();
//			}	
		}
		
		if(svKhaGioi){
			soTCDangKyToiDa += soTCDieuChinh;
		}else if(svYeu){
			soTCDangKyToiDa -=soTCDieuChinh;
		}
		
		if(lvtnYN){
			soTCDangKyToiDa = 7 + soTCLVTN;
			//sv kha gioi dc cong them 2tc
			if(svKhaGioi)
				soTCDangKyToiDa += 2;
		}
		
//		else if(tttnYN){
//			//tttn hk chan la 12 tc
//			if(hkChan)
//				soTCDangKyToiDa = 12 + soTCTTTN;
//			//tttn hk le la 9 TC
//			else
//				soTCDangKyToiDa = 9 + soTCTTTN;
//			//sv kha gioi dc cong them 4tc
//			if(svKhaGioi)
//				soTCDangKyToiDa += 4;
//		}
		
		
		System.out.println(soTCDangKyToiDa + ";" + soTCDieuChinh);
		if(tongSoTCDangKy + soTCDangKy  > soTCDangKyToiDa){
			msg.setCode("ERROR");
			msg.setMsg("Quá số tín chỉ cho phép. Bạn chỉ được DK tối đa " + soTCDangKyToiDa + " tín chỉ!");
			return true;
		}
		return false;
	}
	
	//-----------------------------------------------------------
	//vuot si so lop cho phep
	public static boolean kiemTraVuotSiSoNhomLop(KetQuaDangKy dangKyMoi, Message message) {

		//Vuot si so cho phep
		if(dangKyMoi.getKetqua().getNLMHId().getTKBId().getSiSoMax()> 0 
				&& dangKyMoi.getKetqua().getNLMHId().getTKBId().getSiSoMax() <= dangKyMoi.getKetqua().getNLMHId().getTKBId().getSoHVDangKy()){
			
			//neu nam trong % mo rong
			if(dangKyMoi.getKetqua().getNLMHId().getTKBId().getPhanTramXepLop() > 0 
					&&  dangKyMoi.getKetqua().getNLMHId().getTKBId().getSoHVDangKy() < (dangKyMoi.getKetqua().getNLMHId().getTKBId().getSiSoMax() + dangKyMoi.getKetqua().getNLMHId().getTKBId().getPhanTramXepLop())) {
				message.setCode("NOTICE1");
				message.setMsg("Chờ mở rộng lớp!");
				
			}else{
				//neu nam trong % doi mo lop
				if(dangKyMoi.getKetqua().getNLMHId().getTKBId().getPhanTramDoi() > 0 
						&&  dangKyMoi.getKetqua().getNLMHId().getTKBId().getSoHVDangKy() < (dangKyMoi.getKetqua().getNLMHId().getTKBId().getSiSoMax() + dangKyMoi.getKetqua().getNLMHId().getTKBId().getPhanTramDoi())) {
					message.setCode("NOTICE2");
					message.setMsg("Chờ tách lớp!");
					
				}
				//ngoai cac phan tram doi -> khong cho dang ky
				else{
					message.setCode("ERROR");
					message.setMsg("Quá sĩ số cho phép!");
				}
			}

			return true;
		}
		return false;
	}
	// //////////////////////////////////////////////////////////////////////////////////////
	public static boolean kiemTraTrungMonHoc(KetQuaDangKy dangKyMoi,
			List<KetQuaDangKy> ketQuaDaDangKys) {

		for (KetQuaDangKy ketQuaDangKy : ketQuaDaDangKys) {
			if (ketQuaDangKy.getThoiKhoaBieuDuKien().getNhomLop().getMonHocId()
					.getId().equals(
							dangKyMoi.getThoiKhoaBieuDuKien().getNhomLop()
									.getMonHocId().getId())) {
				return true;
			}
		}
		return false;
	}

	// //////////////////////////////////////////////////////////////////////////////////////
	// kiem tra mon hoc "TIEN QUYET" cua mot mon hoc
	public static boolean kiemTraMonHocTienQuyet(KetQuaDangKy dangKyMoi,List<KetQuaDangKy> ketQuaDaDangKys,
			List<TblThongTinHocVienMonHoc> ketQuahocTaps, ApplicationContext context,Message msg) {
		
		Long monhocId = dangKyMoi.getKetqua().getNLMHId().getMonHocId().getId();
		Long hocvienId = dangKyMoi.getKetqua().getHocVienId().getId();
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		/*****************************************************************
		 * Kiem tra bang mon hoc duoc dk truoc, neu thoa se khong can kiem tra mon tien quyet nua
		 *************************************************************/
		TblDKMHHocVienMonHocDuocDKService tblDKMHHocVienMonHocDuocDKService
			= (TblDKMHHocVienMonHocDuocDKService) context
				.getBean("tbl_DKMH_HocVien_MonHoc_DuocDKClient");
		{
			ConditionReport con = new ConditionReport();
			con.setCol("monHocId.id");
			con.setValue1(monhocId);
			con.setOperator("eq");
			cons.add(con);
		}
		{
			ConditionReport con = new ConditionReport();
			con.setCol("hocVienId.id");
			con.setValue1(hocvienId);
			con.setOperator("eq");
			cons.add(con);
		}
		{
			ConditionReport con = new ConditionReport();
			con.setCol("duocYN");
			con.setValue1(true);
			con.setOperator("eq");
			cons.add(con);
		}
		try {
			int c = tblDKMHHocVienMonHocDuocDKService.counttblDKMHHocVienMonHocDuocDKByCondition(cons);
			if(c > 0){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		String mssv = dangKyMoi.getKetqua().getHocVienId().getMSSV();
		// ----------------------------------------------------------------
		// Tim nhung mon hoc tien quyet cua monhocId
		TblCTDTMonHocTienQuyetService tblCTDTMonHocTienQuyetService = (TblCTDTMonHocTienQuyetService) context
				.getBean("tbl_CTDT_MonHoc_TienQuyetClient");
		
		cons.clear();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("monHoc1Id.id");
			con.setValue1(monhocId);
			con.setOperator("eq");
			cons.add(con);
		}
		if(dangKyMoi.getKetqua().getHocVienId().getChuyenNganhId() != null){ 
	        ConditionReport con = new ConditionReport(); 
	        con.setCol("khoiNganhId.chuyenNganhId.id");
	        con.setValue1(dangKyMoi.getKetqua().getHocVienId().getChuyenNganhId().getId());
	        con.setOperator("eq");
	        cons.add(con);	
	   }
       if(dangKyMoi.getKetqua().getHocVienId() != null ){ 
	        ConditionReport con = new ConditionReport(); 
	        con.setCol("khoiNganhId.khoaHoc");
	        con.setValue1(dangKyMoi.getKetqua().getHocVienId().getKhoaHoc());
	        con.setOperator("eq");
	        cons.add(con);	
	   }

		List<TblCTDTMonHocTienQuyet> monhocTQs = tblCTDTMonHocTienQuyetService
				.findtblCTDTMonHocTienQuyetByCondition(cons, 0, 0);

		// ----------------------------------------------------------------
		// //khong co mon hoc tien quyet
		if (monhocTQs == null || monhocTQs.size() <= 0){
			System.out.println("Khong co mon tien quyet chuyennganhid=" +dangKyMoi.getKetqua().getHocVienId().getChuyenNganhId().getId() + "; khoahoc=" +dangKyMoi.getKetqua().getHocVienId().getKhoaHoc());
			return true;
		}

		// ----------------------------------------------------------------
		// Co mon hoc truoc ,song hanh hoac tien quyet

		for (TblCTDTMonHocTienQuyet monhoctq : monhocTQs) {
			Long monhoctqId = monhoctq.getMonHoc2Id().getId();
			// tim ket qua hoc tap cua monhoctq
			TblThongTinHocVienMonHocService tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService) context
					.getBean("tbl_ThongTin_HocVien_MonHocClient");
			
			String kq = tblThongTinHocVienMonHocService.calSPKiemTraKetQuaMonHoc(mssv, monhoctq.getMonHoc2Id().getMa());
			String loaiTQ = monhoctq.getMa();
			if (loaiTQ.equals("HT")) { // Mon hoc truoc
				// --------------------------------------------
				if (kq == null || kq.isEmpty()) {
					// Chua hoc mon nay
					// -> khong duoc dang ky
					msg.setMsg("Thiếu môn học trước: " 
							+monhoctq.getMonHoc2Id().getMa() + " - " + monhoctq.getMonHoc2Id().getTen());
					return false;

				} 
				//dang ky nhung ko di hoc
				else if (kq.equals("NOTT")) {
					// da dang ky hoc nhung khong di hoc
					// -> khong duoc dang ky
					msg.setMsg("Thiếu môn học trước: " 
							+monhoctq.getMonHoc2Id().getMa() + " - " + monhoctq.getMonHoc2Id().getTen());
					return false;
				} 
				else {
					// PASS - FAIL - UNKN
					// -> OK
				}
				// =============================================
			} else if (loaiTQ.equals("SH")) { // Mon hoc song hanh
				// --------------------------------------------

				// ------------------------------------------
				if (kq == null || kq.isEmpty()) {
					// Chua hoc mon nay
					// Kiem tra da dang ky chua
					if (kiemtraMonHocDangDangKy(monhoctqId, ketQuaDaDangKys)){
						
					}
					else{
						msg.setMsg("Thiếu môn học song hành: " 
								+monhoctq.getMonHoc2Id().getMa() + " - " + monhoctq.getMonHoc2Id().getTen());
						return false;
					}

				} 
				//dang ky nhung khong di hoc
				else if (kq.equals("NOTT")) {
					// da dang ky hoc nhung khong di hoc
					// Kiem tra da dang ky chua
					if (kiemtraMonHocDangDangKy(monhoctqId, ketQuaDaDangKys))
						return true;
					else{
						msg.setMsg("Thiếu môn học song hành: " 
								+monhoctq.getMonHoc2Id().getMa() + " - " + monhoctq.getMonHoc2Id().getTen());
						return false;
					}
				} else {
					// PASS - FAIL - UNKN
					// -> OK
				}
				// =============================================
			} else // TQ tien quyet
			{
				// --------------------------------------------
				if (kq == null || kq.isEmpty()) { 
					// Chua hoc mon nay
					// -> khong duoc dang ky
					msg.setMsg("Thiếu môn học tiên quyết: " 
							+monhoctq.getMonHoc2Id().getMa() + " - " + monhoctq.getMonHoc2Id().getTen());
					return false;

				} 
				//dang ky nhung khong di hoc
				else if (kq.equals("NOTT")) {
					// da dang ky hoc nhung khong di hoc
					// -> khong duoc dang ky
					msg.setMsg("Thiếu môn học tiên quyết: " 
							+monhoctq.getMonHoc2Id().getMa() + " - " + monhoctq.getMonHoc2Id().getTen());
					return false;
				} else if (kq.equals("FAIL")) {
					// da hoc nhung khong dau
					// -> khong duoc dang ky
					msg.setMsg("Thiếu môn học tiên quyết: "  
							+monhoctq.getMonHoc2Id().getMa() + " - " + monhoctq.getMonHoc2Id().getTen());
					return false;
				} else if (kq.equals("UNKN")) {
					// UNKN
					// -> warning
					msg.setCode("NOTICE");
					msg.setMsg("Môn tiên quyết " +monhoctq.getMonHoc2Id().getMa() + "-" 
							+ monhoctq.getMonHoc2Id().getTen() 
							+ " chưa có điểm, nếu sau đợt DK kết quả điểm là KHÔNG ĐẠT thì kết quả đăng ký sẽ bị hủy!!!");
				}else{
					//pass
					//->ok
				}
				// =============================================
			}
		}//end for
		return true;
	}

	static Boolean kiemtraMonHocDangDangKy(Long monhocId,
			List<KetQuaDangKy> ketQuaDaDangKys) {
		Boolean dadangky = false;
		// kiem tra da dang ky chua
		for (KetQuaDangKy dangky : ketQuaDaDangKys) {
			if (dangky.getKetqua().getNLMHId().getMonHocId().getId().equals(
					monhocId)) {
				dadangky = true;
				break;
			}
		}
		return dadangky;
	}

	// //////////////////////////////////////////////////////////////////////////////////////
	// kiem tra mon hoc "THAY THE" cua mot mon hoc
	public static List<TblCTDTThayTheMonHoc> kiemTraMonHocThayThe(
			KetQuaDangKy dangKyMoi, ApplicationContext context) {
		Long monhocId = dangKyMoi.getKetqua().getNLMHId().getMonHocId().getId();

		// tim nhung mon hoc thay the cho mon hoc
		// List<Long> thaythes = new ArrayList<Long>();
		TblCTDTThayTheMonHocService tblCTDTThayTheMonHocService = (TblCTDTThayTheMonHocService) context
				.getBean("tbl_CTDT_ThayTheMonHocClient");
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("monHoc1Id.id"); // MonHoc2Id
			con.setValue1(monhocId);
			con.setOperator("eq");
			cons.add(con);
		}
		List<TblCTDTThayTheMonHoc> monhocthaythes = tblCTDTThayTheMonHocService
				.findtblCTDTThayTheMonHocByCondition(cons, 0, 0);

		return monhocthaythes; // MonHocThayThe1Id , MonHocThayThe2Id
	}

	// //////////////////////////////////////////////////////////////////////////////////////
	// kiem tra "nhom lop mon hoc" co dung tbl_RangBuoc_ChuyenNganh_Cam
	// Cam true ; OK false
	public static Boolean kiemTraCamNhomLop(KetQuaDangKy dangKyMoi,
			ApplicationContext context) {
		
		Long monhocId = dangKyMoi.getKetqua().getNLMHId().getMonHocId().getId();
		
		String nhomlop = parseNhomLop(dangKyMoi.getKetqua().getNLMHId().getMa());
		
		Long chuyennganhId = dangKyMoi.getKetqua().getHocVienId()
				.getChuyenNganhId().getId();
		
		Integer khoahoc = dangKyMoi.getKetqua().getHocVienId().getKhoaHoc();
		
		TblRangBuocChuyenNganhCamService tblRangBuocChuyenNganhCamService = (TblRangBuocChuyenNganhCamService) context
				.getBean("tbl_RangBuoc_ChuyenNganh_CamClient");
		
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{ // nhom hoc
			ConditionReport con = new ConditionReport();
			con.setCol("nhomHoc");
			con.setValue1(nhomlop);
			con.setOperator("eq");
			cons.add(con);
		}
		{
			// chuyennganh
			ConditionReport con = new ConditionReport();
			con.setCol("chuyenNganhId.id");
			con.setValue1(chuyennganhId);
			con.setOperator("eq");
			cons.add(con);
		}
		{
			// khoa hoc
			ConditionReport con = new ConditionReport();
			con.setCol("khoaHoc");
			con.setValue1(khoahoc.toString());
			con.setOperator("eq");
			cons.add(con);
		}

		int countCam = tblRangBuocChuyenNganhCamService.counttblRangBuocChuyenNganhCamByCondition(cons);
		// Cam : count > 0 -> true
		// Ko Cam: count = 0 -> false
		return (countCam > 0) ? true : false;
	}

	static String parseNhomLop(String str) {
		return str.substring(0, 2);
	}
	
	// //////////////////////////////////////////////////////////////////////////////////////
}
